package cn.tao.order.service.impl;

import cn.tao.base.constant.PayBillConstants;
import cn.tao.base.service.impl.BaseServiceImpl;
import cn.tao.order.domain.OrderAddress;
import cn.tao.order.domain.OrderAdopt;
import cn.tao.order.mapper.OrderAddressMapper;
import cn.tao.order.mapper.OrderAdoptMapper;
import cn.tao.order.service.IOrderAdoptService;
import cn.tao.pay.domain.PayBill;
import cn.tao.pay.mapper.PayBillMapper;
import cn.tao.pet.domain.Pet;
import cn.tao.pet.mapper.PetMapper;
import cn.tao.user.domain.User;
import cn.tao.user.domain.UserAddress;
import cn.tao.user.mapper.UserAddressMapper;
import cn.tao.utils.CodeGenerateUtils;
import cn.tao.utils.LoginContext;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

@Service
public class OrderAdoptServiceImpl extends BaseServiceImpl<OrderAdopt> implements IOrderAdoptService {
    @Autowired
    private OrderAdoptMapper orderAdoptMapper;

    @Autowired
    private UserAddressMapper userAddressMapper;

    @Autowired
    private OrderAddressMapper orderAddressMapper;

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private PayBillMapper payBillMapper;
    //创建订单
    @Override
    @Transactional
    public void createOrder(Map<String, String> map, HttpServletRequest request) {
        Long addressId = Long.valueOf(map.get("addressId"));
        String payway = map.get("payway");
        Long petId = Long.valueOf(map.get("petId"));

        //通过工具类拿到当前登录用户user 领养的用户
        User user = (User) LoginContext.getCurrentUser(request);
        /*
            1.更改宠物状态，为售出
            2.生成一个OrderAdopt对象
            3.根据addressId 查询userAddress对象
            4.生成一个orderAddress
            5.保存 orderadopt orderAddress
            6.根据订单生成支付单paybill
         */
        Pet pet = petMapper.loadById(petId);
        //更改为售出状态
        pet.setState(2);
        petMapper.update(pet);
        //生成一个orderAdopt对象
        OrderAdopt orderAdopt =createAdopt(pet,user);
        //根据addressId查询userAddress对象
        UserAddress userAddress = userAddressMapper.loadById(addressId);
        //根据用户地址对象生成订单对象
        OrderAddress orderAddress = userAddress2OrderAddress(userAddress);
        //设置订单编号
        orderAddress.setOrderSn(orderAdopt.getOrderSn());
        //保存对象
        orderAddressMapper.save(orderAddress);
        orderAdopt.setAddress_id(orderAddress.getId());
        orderAdoptMapper.save(orderAdopt);
        PayBill payBill =orderAdopt2PayBill(orderAdopt);
        //设置支付方式
        payBill.setPayChannel(payway);
        payBillMapper.save(payBill);



    }

    private PayBill orderAdopt2PayBill(OrderAdopt orderAdopt) {
        PayBill payBill = new PayBill();
        BeanUtils.copyProperties(orderAdopt,payBill);
        payBill.setMoney(orderAdopt.getPrice());
        //设置订单类型
        payBill.setBusinessType(PayBillConstants.ORDER_ADOPT);
        //订单主键
        payBill.setBusinessKey(orderAdopt.getId());
        return payBill;
    }

    //把哟个useraddress对象转为orderaddress对象
    private OrderAddress userAddress2OrderAddress(UserAddress userAddress) {
        OrderAddress orderAddress = new OrderAddress();
        BeanUtils.copyProperties(userAddress,orderAddress);
        return orderAddress;
    }

    private OrderAdopt createAdopt(Pet pet,User user) {
        OrderAdopt orderAdopt = new OrderAdopt();
        orderAdopt.setDigest("领养订单");
        //0 待支付 1 支付成功 -1 取消订单
        orderAdopt.setState(1);
        //价格
        orderAdopt.setPrice(pet.getSaleprice());
        //订单编号
        orderAdopt.setOrderSn(CodeGenerateUtils.generateOrderSn(user.getId()));
        //private String paySn;
        //用当前时间戳加上往后半小时 来设置支付时间
        orderAdopt.setLastPayTime(new Date(System.currentTimeMillis()+1000*60*30));
        //private Date lastConfirmTime;
        orderAdopt.setPet_id(pet.getId());
        orderAdopt.setUser_id(user.getId());
        orderAdopt.setShop_id(pet.getShop_id());
        //进入order——address表中的数据
        //private Long address_id;
        return  orderAdopt;
    }
}
